<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>分类参数</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
              <el-alert title="注意：只允许为第三级分类设置相关参数" type="warning" show-icon></el-alert>
              <el-row class='cat-opt'>
                  <el-col>
                    <span>选择商品分类：</span>
                        <el-cascader
                            expand-trigger="hover"
                            v-model="selectKeys"
                            :options="cateList"
                            :props="cateProps"
                            @change="handleChange">
                        </el-cascader>
                  </el-col>
              </el-row>
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="动态参数" name="first" >
                        <el-button type="primary" :disabled="isBtnDisabled" @click="add">添加参数</el-button>
                        <el-table :data="firstData" stripe border>
                             <el-table-column type="expand">
                                 <template v-slot="scope">
                                     <el-tag v-for="(item,index) in scope.row.attrvalue" :key="index" style="margin-right:10px;" closable @close="closeTag(index,scope.row)">{{item}}</el-tag>
                                     <el-input
                                        class="input-new-tag"
                                        v-if="scope.row.status"
                                        v-model="scope.row.value"
                                        ref="saveTagInput"
                                        size="small"
                                        @keyup.enter.native="handleInputConfirm(scope.row)"
                                        @blur="handleInputConfirm(scope.row)"
                                    >
                                    </el-input>
                                    <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                                 </template>
                             </el-table-column>
                            <el-table-column type="index"></el-table-column>
                            <el-table-column label="参数名称" prop="attr"></el-table-column>
                            <el-table-column label="操作">
                                <template v-slot="scope">
                                    <el-button size="mini" type='primary' icon="el-icon-edit"></el-button>
                                    <el-button size="mini" type='danger' icon="el-icon-delete"></el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-tab-pane>

                    <el-tab-pane label="静态属性" name="second" >
                        <el-button type="primary" :disabled="isBtnDisabled" @click="add">添加属性</el-button>
                            <el-table :data="secondData" stripe border>
                                <el-table-column type="index"></el-table-column>
                                <el-table-column label="属性名称" prop="attr"></el-table-column>
                                <el-table-column label="操作">
                                    <template v-slot="scope">
                                        <el-button size="mini" type='primary' icon="el-icon-edit"></el-button>
                                        <el-button size="mini" type='danger' icon="el-icon-delete"></el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                    </el-tab-pane>
                </el-tabs>
        </el-card>
        <!-- 添加参数的对话框 -->
        <el-dialog
            :title="'添加' + titleText"
            :visible.sync="dialogVisible"
            width="50%"
            @close="close">
           <el-form ref="formRef" :model="addForm" :rules="addRules" label-width="80px">
                <el-form-item :label="titleText" prop="attr">
                    <el-input v-model="addForm.attr"></el-input>
                </el-form-item>
           </el-form>    
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addClass">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>
<script>
export default {
    data() {
        return {
            cateList:[
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
                {cate_name:'热门推荐',type:'true',order:'1',children:[{cate_name:'彩电',order:'2',children:[{cate_name:'小米电视'},{cate_name:'彩虹电视'}]},{cate_name:'冰箱',},{cate_name:'洗衣机'}]},
            ],
            cateProps:{
                label:'cate_name',
                value:'cate_name',
                children:'children'
            },
            selectKeys:[],//级联选择框选双向绑定的数组
            activeName:'first',
            firstData: [],
            secondData: [],
            dialogVisible:false,
            addForm:{
                attr:'',
            },
            addRules:{
                attr:[
                    { required: true, message:'请输入参数名称', trigger: 'blur' }
                ]
            },
        }
    },
    created() {

    },
    
    methods : {
        handleChange() {
            console.log(this.selectKeys)
            if(this.selectKeys.length !== 3) {
                 this.selectKeys = []
                 this.firstData = []
                 this.secondData = []
                 return 
            }
            this.getData()
        },
        handleClick() {
             if(this.selectKeys.length !== 3) {
                 this.selectKeys = []
                 return 
            }
            this.getData()
        },
        getData() {
            this.firstData = [{attr:'版式',attrvalue:['24英寸高清显示屏','21英寸高清显示屏','27英寸高清显示屏'],status:false,value:''}]
            this.secondData = [
                {attr:'主题',},
                {attr:'显示'},
                {attr:'名称'},
                {attr:'外观'},
                {attr:'功耗'},
            ]
        },
        add() {
            this.dialogVisible = true
        },
        close() {
            this.$refs.formRef.resetFields()
        },
        addClass() {
            this.$refs.formRef.validate(valid => {
                if(!valid) return
                if(this.activeName == 'first'){
                    var data = {attr:this.addForm.attr,attrvalue:[],status:false,value:''}
                    this.firstData.push(data)
                }else{
                    var data = {attr:this.addForm.attr}
                    this.secondData.push(data)
                }
                this.dialogVisible = false
                this.$message.success('添加成功')
            })
        },
        handleInputConfirm(item) {
            if(item.value.trim().length === 0){
                item.status = false;
                item.value = '';
                return
            }
            item.attrvalue.push(item.value);
            item.status = false;
            item.value = '';
        },
        showInput(row) {
            row.status = true;
            this.$nextTick(_ => {
                 this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        closeTag(index,row) {
            console.log(row)
           row.attrvalue.splice(index,1)
        }
    },
    computed : {
        isBtnDisabled() {
            if(this.selectKeys.length !== 3){
                return true
            }
            return false
        } ,
        titleText() {
            if(this.activeName == 'first'){
                return  '参数名称'
            }
            return '属性名称'
        }
    }
}
</script>
<style lang="less" scoped>
.cat-opt {
    margin:15px 0
}

.input-new-tag {
    width:120px;
}
</style>